﻿using DatabaseCore.Dao.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DatabaseCore.Dao
{
    public class PlantDomExportDao : IPlantDomExportDao
    {
        public string GetPlantExport(string PlantDom)
        {
            using (var ctx = new ExportDBEntities())
            {
                var plantExport = (from x in ctx.tbl_PlantDomExport
                                   where x.PlantDom.Equals(PlantDom,StringComparison.InvariantCultureIgnoreCase)
                                  select x.PlantExport).FirstOrDefault();

                return plantExport;
            }
        }


        public List<tbl_PlantDomExport> GetAllPlant()
        {
            using (var ctx = new ExportDBEntities())
            {
                var plantExport = (from x in ctx.tbl_PlantDomExport
                                   select x).ToList();

                return plantExport;
            }
        }

        public List<tbl_PlantDomExport> GetAllPlantByCountryExport(string orderno)
        {
            using (var ctx = new ExportDBEntities())
            {
                var country = (from d in ctx.tbl_Order
                                  where d.OrderNo == orderno
                                  select d.PortOfDestination).FirstOrDefault();
                var allCountry = (from c in ctx.CountryDestination
                                 select c).ToList();

                var location = (from cs in allCountry
                               where cs.Country.ToLower().Trim() == country.ToLower().Trim()
                               select cs.PlanLocation).FirstOrDefault();
                var planAvaliable = from p in ctx.tbl_PlantDomExport
                                    where p.Location == location
                                    select p;

                return planAvaliable.ToList();
            }
        }

        public List<tbl_PlantDomExport> GetPlantByLocation(string Location)
        {
            using (var ctx = new ExportDBEntities())
            {
                var plantExport = (from x in ctx.tbl_PlantDomExport
                                   where x.Location.Equals(Location, StringComparison.InvariantCultureIgnoreCase)
                                   select x).ToList();

                return plantExport;
            }
        }
    }
}
